<!DOCTYPE html>
<!--
Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/EmptyPHPWebPage.php to edit this template
-->
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
$first=!count($_POST);
if($first){
    //第一次访问 页面进行渲染
 //   echo "是第一次访问";
?>    
   <h1>用户添加</h1>
        <form action="adduser.php" method="post">
            用户名:<input type="text" name="username"/><br/>
            密 &nbsp;&nbsp;码:<input type="password" name="upwd"/><br/>
            年 &nbsp;&nbsp;龄:
            <select name="age"/>
                <?php
                for($a=10;$a<100;$a++){
                    if ($a==20){
                    echo "<option selected=\"selected\">${a}</option>";
                    } else {
                        echo "<option>${a}</option>";   
                    }
                        
                }
                ?>
            </select>
            <br/>
            性 &nbsp;&nbsp;别:
            <select name="sex"/>
                <option>男</option>
                <option selected="selected">女</option>
                <option>保密</option>
            </select>
            <br/>
            <input type="submit" value="确定"/>
            <input type="submit" value="取消"/>
        </form>
<?php
} else {
    //不是第一次访问，进行数据处理
     $uid=$_POST["uid"];
     $name=$_POST["username"];
     $pwd=$_POST["upwd"];
     $age=$_POST["age"];
     $sex=$_POST["sex"];
     $hostname="127.0.0.1";
     $username="root";
     $password="";
     $database="mybb_text";
     $connect=mysqli_connect($hostname, $username, $password, $database);
     if($connect){
//           echo "数据库连接成功！";
           // mysqli_set_charset 设置连接使用的字符集
         mysqli_set_charset($connect,"utf-8");
         $sqlstring="INSERT INTO `mybb_text`.`user_yi` (`uid`, `username`, `upwd`, `age`, `sex`) "
                . "VALUES ('${uid}','${name}', '${pwd}', '${age}', '${sex}')";
            //mysql_query 执行sql语句并获得执行结果
        // $sqlstring="select * from user_yi";
         $result=mysqli_query($connect, $sqlstring);
         if($result){
             echo "<script>alert('添加成功！');"
                  . "location='mysql001.php';</script>";
        }else{
             echo "<script>alert('添加失败,请重试！！');"
                  . "location='mysql001.php';</script>";        
                    }
        }
mysqli_close($connect);

}
?>     
    </body>
</html>
